package com.cf.base.mvp.mvp;

import androidx.lifecycle.Lifecycle;

import com.cf.base.mvp.rx.LifecycleTransformer;
import com.cf.base.mvp.rx.RxLifecycle;

import io.reactivex.subjects.BehaviorSubject;

/**
 * @param <P> 契约Presenter
 * @author yangji
 */
public abstract class BaseModel<P extends IPresenter> implements IModel<P> {

    private P presenter;
    private BehaviorSubject<Lifecycle.Event> subject;

    @Override
    public void bindPresenter(P presenter, BehaviorSubject<Lifecycle.Event> subject) {
        this.presenter = presenter;
        this.subject = subject;
    }

    protected P getPresenter() {
        return presenter;
    }

    protected BehaviorSubject<Lifecycle.Event> getSubject() {
        return subject;
    }

    protected <T, R> LifecycleTransformer<T> bindEvent() {
        return RxLifecycle.bindEvent(getSubject(), Lifecycle.Event.ON_DESTROY);
    }

    protected <T, R> LifecycleTransformer<T> bindEvent(Lifecycle.Event event) {
        return RxLifecycle.bindEvent(getSubject(), event);
    }
}
